package day_2023_4_to_7;

/**
 * @author haomin
 * @date 2023/04/18 20:59
 **/
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

public class Day4_18_Num1026 {
    class Solution {
        int ans = 0;
        public int maxAncestorDiff(TreeNode root) {
            if(root == null) return ans;
            dfs(root, root.val, root.val);
            return ans;
        }
        public void dfs(TreeNode root, int max, int min) {
            if(root == null) return;
            max = Math.max(max, root.val);
            min = Math.min(min, root.val);
            ans = Math.max(ans, Math.abs(max-min));
            dfs(root.left, max, min);
            dfs(root.right, max, min);
        }
    }
}